package tq.com.tqstudent.ui.activity.classes;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.lib.common.base.BaseActivity;
import com.lzy.okgo.OkGo;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;

import butterknife.BindView;
import tq.com.tqstudent.App;
import tq.com.tqstudent.Constants;
import tq.com.tqstudent.R;
import tq.com.tqstudent.adapter.common.CommonAdapter;
import tq.com.tqstudent.adapter.common.ViewHolder;
import tq.com.tqstudent.bean.BaseBean;
import tq.com.tqstudent.bean.MalistBean;
import tq.com.tqstudent.net.ok.DialogCallback;
import tq.com.tqstudent.utils.VarietyUtil;

/**
 * 微课列表
 * @Author rames
 * @date 2019/3/6 14:47
 */
public class MicroListActivity extends BaseActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.empty_layout)
    LinearLayout emptyLayout;
    private CommonAdapter adapter;
    private int index;
    private int pageNO = 1;

    public static void show(Activity activity, int id){
        Intent intent = new Intent(activity, MicroListActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("index", id);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    @Override
    protected void getExtras(Bundle extras) {
        this.index = extras.getInt("index");
    }

    @Override
    public int initLayout() {
        return R.layout.activity_recyclerview;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        setTitle("微课");

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new CommonAdapter<MalistBean>(this, R.layout.item_micro_list, new ArrayList()) {
            @Override
            protected void convert(ViewHolder holder, MalistBean bean, int position) {
                holder.setText(R.id.view_count_tv, bean.view_cnt);
                holder.setText(R.id.title_tv, bean.title);
                Glide.with(MicroListActivity.this).load(App.getFileUrl(bean.content_cover)).apply(VarietyUtil.getGlideHeadOptions(R.mipmap.icon_micro_video_cover)).into((ImageView) holder.getView(R.id.cover_iv));
            }
        };
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener((view, holder, position) -> {
            MalistBean bean = (MalistBean) adapter.getDatas().get(position);
            VideoPlayActivity.show(MicroListActivity.this, index, bean.id, bean.title, App.getFileUrl(bean.content), bean.id);
        });

        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNO++;
                getData();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNO = 1;
                getData();
            }
        });
    }

    @Override
    public void initData() {
        refreshLayout.autoRefresh();
    }

    private void getData() {
        String uri = "api/clz/malist/"+index+"/"+ Constants.PAGE_SIZE+"/"+pageNO;
        OkGo.<BaseBean<ArrayList<MalistBean>>>get(App.getHttpUrl(uri))
                .tag(this)
                .execute(new DialogCallback<ArrayList<MalistBean>>(MicroListActivity.this) {
                    @Override
                    public void handleSuccess(ArrayList<MalistBean> data) {
                        // 这是加载更多
                        if(refreshLayout.getState() == RefreshState.Loading){
                            adapter.addData(data);
                            refreshLayout.finishLoadMore();
                            if(data.size() < Constants.PAGE_SIZE){
                                refreshLayout.setEnableLoadMore(false);
                            }

                            // 这是下拉刷新
                        } else {
                            adapter.getDatas().clear();
                            adapter.setData(data);
                            refreshLayout.finishRefresh();
                            refreshLayout.setEnableLoadMore(true);
                        }

                        emptyLayout.setVisibility(recyclerView.getAdapter().getItemCount() > 0 ? View.GONE : View.VISIBLE);
                    }

                    @Override
                    public void handleFail(BaseBean body) {
                        super.handleFail(body);
                        refreshLayout.finishLoadMore();
                        refreshLayout.finishRefresh();
                    }
                });
    }
}
